package com.situ.student.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.situ.student.dao.IStudentDao;
import com.situ.student.entity.Student;
import com.situ.student.util.JDBCUtil;

/**
 * 用JDBC来实现DAO
 */
public class StudentDaoJdbcImpl implements IStudentDao {

	@Override  //插入
	public int insert(Student student) {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		/**
		 * 打印列表
		 */
		int count = 0;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "insert into student(name,age,gender,banji) VALUES (?,?,?,?);";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setObject(1, student.getName());
			preparedStatement.setObject(2, student.getAge());
			preparedStatement.setObject(3, student.getGender());
			preparedStatement.setObject(4, student.getBanji());
			count = preparedStatement.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return count;
	}
	
	/**
	 * 插入
	 */

	@Override
	public List<Student> list() {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		List<Student> list = new ArrayList<>();
		try {
			connection = JDBCUtil.getConnection();
			String sql = "SELECT id,name,age,gender,banji FROM student";
			// 预编译sql
			preparedStatement = connection.prepareStatement(sql);
			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {
				Integer id = resultSet.getInt("id");
				String name = resultSet.getString("name");
				Integer age = resultSet.getInt("age");
				String gender = resultSet.getString("gender");
				String banji = resultSet.getString("banji");
				Student student = new Student(id, name, age, gender,banji);
				list.add(student);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, preparedStatement, resultSet);
		}
		return list;
	}

	/**
	 * 根据ID删除
	 */
		
	@Override
	public int deleteById(Integer id) {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		int count = 0;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "delete from student where id = ? ";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setInt(1, id);
			count = preparedStatement.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return count;
	}
	
	/**
	 * 根据名字查找
	 */
	@Override
	public List<Student> findByName(String name) {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		List<Student> list = new ArrayList<>();
		try {
			connection = JDBCUtil.getConnection();
			String sql = "SELECT * FROM student WHERE NAME = ?;";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setString(1, name);
			preparedStatement.executeQuery();
			ResultSet resultSet = null;
			Integer id = resultSet.getInt("id");
			name = resultSet.getString("name");
			Integer age = resultSet.getInt("age");
			String gender = resultSet.getString("gender");
			String banji = resultSet.getString("banji");
			Student student = new Student(id, name, age, gender,banji);
			list.add(student);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}


}
